/**
 * Copyright 2009 University of Washington This code is released under the GPL version 3 - with a copy of the license
 * included
 * 
 * @author Maya Hao Li
 */
package seedpod.webapp.view;

import java.io.File;
import java.util.Date;
import java.util.Vector;

import seedpod.model.custom.SeedpodUser;
import seedpod.webapp.LimsContext;
import seedpod.webapp.Seedpod;
import seedpod.webapp.view.htmlwidget.HtmlLink;
import seedpod.webapp.view.htmlwidget.Image;

/**
 * Utility object that prints header, footer, menu of a Seedpod page. Override these methods for customization
 * 
 * @author maya
 */
public class Page {

	protected Vector<String>	_cssPages;
	protected Vector<String>	_jsLibrary;
	protected StringBuffer		_header				= new StringBuffer();
	protected String			_currPath;
	protected String			_title				= "Seedpod LIMS";

	public String				DEFAULT_CSS			= "/stylesheet/sp_main.css";
	public String				DEFAULT_JS			= "/javascript/lib/prototype.js";
	public String				DEFAULT_PAGE_ICON	= "/images/sp_icon.gif";

	public Page() {
		_cssPages = new Vector<String>();
		_jsLibrary = new Vector<String>();
		_header = new StringBuffer();

		_cssPages.add( DEFAULT_CSS );
		_jsLibrary.add( DEFAULT_JS );
		_currPath = "";
	}

	public Page(String currentPath) {
		this();
		_currPath = currentPath;
	}

	public void addJsLibrary( String libPath ) {
		_jsLibrary.add( libPath );
	}

	public String getBanner( SeedpodUser user ) {
		StringBuffer b = new StringBuffer();
		String home = Seedpod.getAbsoluteUrl( LimsContext.HOME_URL );
		Date now = new Date();
		Image icon = new Image( "icon", Seedpod.getAbsoluteUrl( DEFAULT_PAGE_ICON ) );

		b.append( "<div id=\"banner\">" );

		b.append( "<div id=\"icon\">" + icon.render() + "</div>" );
		b.append( "<div id=\"LIMSTitle\">" + LimsContext.getLimsName() + "</div>" );

		b.append( "<div id=\"topMenuBar\">" );

		b.append( "<ul id=\"topMenu\">" );
		b.append( "<li id=\"home\"><a class=\"menuItemLink\" href=\"" + home + "\">Home</a></li>" );

		if ( user != null ) {
			b.append( "<li id=\"user\">" + getUserLoginLinks( user ) + "</li>" );
			b.append( "<li id=\"logout\">" + getLogoutUrl( user ) + "</li>" );
		} else {
			String signinUrl = Seedpod.getAbsoluteUrl( LimsContext.getLoginUrl() );
			b.append( "You are not signed in yet. " );
			b.append( "<a class=\"menuItemLink\" href=\"" + signinUrl + "\">Sign in</a>" );
		}

		b.append( getUserPageLinks() );

		b.append( "<li id=\"currentTime\" >" + now.toString() + "</li>" );
		b.append( "</ul>" );
		b.append( "</div>" ); // end topMenuBar div
		// time & date
		b.append( "</div> " ); // end banner div
		return b.toString();
	}

	public String getFooter() {
		return "<div id=\"footer\" >Administrator: haoli@u.washington.edu</div>";
	}

	private StringBuffer getHeader() {
		for ( String css : _cssPages ) {
			css = Seedpod.getAbsoluteUrl( css );
			_header.append( "<link href=\"" + css + "\" type=\"text/css\" rel=\"stylesheet\">" );
		}

		_header.append( "<LINK REL=\"SHORTCUT ICON\"       HREF=\"logo.ico\">" );

		for ( String js : _jsLibrary ) {
			js = Seedpod.getAbsoluteUrl( js );
			_header.append( "<script type=\"text/JavaScript\" src=\"" + js + "\"></script>" );
		}
		return _header;
	}

	public String getLimsTitle() {
		return "<div id=\"LIMSTitle\">" + LimsContext.getLimsName() + "</div>";
	}

	private String getLogoutUrl( SeedpodUser user ) {
		String logoutUrl = Seedpod.getAbsoluteUrl( LimsContext.getLogoutUrl() );
		HtmlLink logouthtml = new HtmlLink( logoutUrl, "Logout" );
		logouthtml.setCssClass( "menuItemLink" );
		return user != null ? logouthtml.render() : "";
	}

	public String getPageEndHtml() {
		String html = "</body>";
		html += "</html>";
		return html;
	}

	public String getPageOpenHtml() {
		StringBuffer hb = new StringBuffer();
		hb.append( "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">" );
		hb.append( "<html>" );
		hb.append( "<head>" );
		hb.append( "<title>" + _title + "</title>" );
		hb.append( "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\">" );
		hb.append( getHeader() );
		hb.append( "</head>" );
		hb.append( "<body>" );
		return hb.toString();
	}

	private StringBuffer getUserLoginLinks( SeedpodUser user ) {
		StringBuffer b = new StringBuffer();
		String userUrl = Seedpod.getAbsoluteUrl( "/lims/instance.jsp?oid=" + user.getOid() );
		b.append( "<a class=\"menuItemLink\" href=\"" + userUrl + "\">" );
		b.append( user.getUserName() );
		b.append( "</a>" );
		return b;
	}

	private StringBuffer getUserPageLinks() {
		StringBuffer b = new StringBuffer();
		String dir = LimsContext.getUserPageDirectory();
		String pageDirPath = LimsContext.getRealPath( dir );
		File pagesFolder = new File( pageDirPath );

		if ( pagesFolder != null && pagesFolder.isDirectory() ) {
			File[] files = pagesFolder.listFiles();
			for ( File file : files ) {
				String fname = file.getName();

				String absUrl = Seedpod.getAbsoluteUrl( dir + "/" + fname );
				String displayName = fname.substring( 0, fname.lastIndexOf( "." ) );
				b.append( "<li><a class=\"menuItemLink\" href=\"" + absUrl + "\">" + displayName + "</a></li>" );
			}
		}
		return b;
	}

	public void insertHeader( String newHeader ) {
		_header.append( newHeader );
	}

	public void setTitle( String title ) {
		_title = title;
	}

	public void useDefaultLibrary() {
		_jsLibrary.add( DEFAULT_JS );
		_cssPages.add( DEFAULT_CSS );
	}

}
